<!--预约信息管理-->
<template>
  <div class="app-container">
    <el-tabs :tab-position="tabPosition" style="height: 800px;">
      <el-tab-pane label="在线预约">
        <el-card>
          在线预约
        </el-card>
        <el-card>
            <el-form ref="form" :model="yyxx_form" :rules="rules" label-width="80px">
              <el-form-item label="客户编号" prop="customerId">
                <el-input v-model="yyxx_form.customerId" placeholder="请输入客户编号" readonly disabled/>
              </el-form-item>
              <el-form-item label="手机号" prop="phoneNum">
                <el-input style="width: 300px" v-model="yyxx_form.phoneNum" placeholder="根据手机号查询客户编号" />
                <el-button type="primary" style="cursor: pointer" @click="queryCustomerIdByPhone">查询</el-button>
              </el-form-item>
              <el-form-item label="预约时间" prop="appointmentTime">
                <el-date-picker
                  v-model="yyxx_form.appointmentTime"
                  align="right"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="服务类型" prop="serviceType">
                <el-select v-model="yyxx_form.serviceType" placeholder="请选择">
                  <el-option
                    v-for="item in serviceTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="是否取消" prop="isCanceled">
                <el-input v-model="yyxx_form.isCanceled" readonly disabled placeholder="预约是否被取消" />
              </el-form-item>
              <el-form-item label="技师编号" prop="technicianId">
                <el-input  v-model="yyxx_form.technicianId" disabled  placeholder="负责该预约的技师编号[确认预约后自动分配]" readonly/>
              </el-form-item>
            </el-form>
          <el-button type="primary" @click="submitForm">提 交</el-button>
<!--          <el-button type="danger" @click="reset">重 置</el-button>-->
<!--          <el-button @click="cancel">取 消</el-button>-->
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="预约管理">
        <el-card>
          预约管理
        </el-card>
        <el-card>
          <div>
            <el-form :model="queryYYXX_Params" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="客户编号" prop="customerId">
                <el-input style="width: 300px" v-model="queryYYXX_Params.customerId" placeholder="请输入客户编号"/>
              </el-form-item>
              <el-form-item label="手机号" prop="phoneNum">
                <el-input style="width: 200px" v-model="queryYYXX_Params.phoneNum" placeholder="根据手机号查询客户编号" />
                <el-button type="primary" style="cursor: pointer" @click="queryCustomerIdByPhone2">查询</el-button>
              </el-form-item>
              <el-form-item label="预约时间" prop="appointmentTime">
                <el-date-picker
                  v-model="queryYYXX_Params.appointmentTime"
                  align="right"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                  >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="服务类型" prop="serviceType">
                <el-select v-model="queryYYXX_Params.serviceType" placeholder="请选择">
                  <el-option
                    v-for="item in serviceTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="是否取消" prop="isCanceled">
                <el-select v-model="queryYYXX_Params.isCanceled" placeholder="请选择">
                  <el-option
                    v-for="item in isCanceledOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
<!--                <el-input v-model="queryYYXX_Params.isCanceled"   placeholder="预约是否被取消" />-->
              </el-form-item>
              <el-form-item label="技师编号" prop="technicianId">
                <el-input style="width:300px;" v-model="queryYYXX_Params.technicianId"   placeholder="负责该预约的技师编号[确认预约后自动分配]" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['system:customer_appointments:edit']"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['system:customer_appointments:remove']"
                >删除</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="customer_appointmentsList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="客户编号" align="center" prop="customerId" />
              <el-table-column label="预约时间" align="center" prop="appointmentTime" />
              <el-table-column label="服务类型" align="center" prop="serviceType" />
              <el-table-column label="是否取消" align="center" prop="isCanceled" />
              <el-table-column label="技师编号" align="center" prop="technicianId" />
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:customer_appointments:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['system:customer_appointments:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total>0"
              :total="total"
              :page-sizes=pageSizes
              :page.sync="queryYYXX_Params.pageNum"
              :limit.sync="queryYYXX_Params.pageSize"
              @pagination="getList"
            />
            <!-- 添加或修改客户预约信息对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
              <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="客户编号" prop="customerId">
                  <el-input v-model="form.customerId" placeholder="请输入客户编号" disabled/>
                </el-form-item>

                <el-form-item label="预约时间" prop="appointmentTime">
                  <el-date-picker
                    v-model="form.appointmentTime"
                    align="right"
                    type="date"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="服务类型" prop="serviceType">
                  <el-select v-model="form.serviceType" placeholder="请选择">
                    <el-option
                      v-for="item in serviceTypeOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="是否取消" prop="isCanceled">
                  <el-select v-model="form.isCanceled" placeholder="请选择">
                    <el-option
                      v-for="item in isCanceledOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                  <!--                <el-input v-model="queryYYXX_Params.isCanceled"   placeholder="预约是否被取消" />-->
                </el-form-item>

                <el-form-item label="技师编号" prop="technicianId">
                  <el-input v-model="form.technicianId" placeholder="请输入负责该预约的技师编号" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFormYYGL">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
            </el-dialog>
          </div>
        </el-card>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>

import {getInfo, getCustomerId, submitYyxx, make_sure_submit} from "@/api/4s_shop/info";
import { listCustomer_appointments, getCustomer_appointments, delCustomer_appointments, addCustomer_appointments, updateCustomer_appointments } from "@/api/4s_shop/customer_appointments";

export default {
  data() {
    return {
      pageSizes:[7],
      // 表单参数
      form: {},
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户预约信息表格数据
      customer_appointmentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      loading:true,
      // 查询参数
      queryYYXX_Params: {
        pageNum: 1,
        pageSize:7,
        customerId: null,
        appointmentTime: null,
        serviceType: null,
        isCanceled: null,
        technicianId: null
      },
      serviceTypeOptions:[{
        value: '0',
        label: '保养'
      }, {
        value: '1',
        label: '维修'
      },{
        value: '2',
        label: '检测'
      }],
      isCanceledOptions:[{
        value: '0',
        label: '未取消'
      },{
        value: '1',
        label: '已取消'
      },],
      isCanceledTableShow:{
        "0":'未取消',
        "1":'已取消'
      },
      serviceTypeTableShow:{
        "0":"保养",
        "1":"维修",
        "2":"检测"
      },
      tabPosition: 'left',
      // 表单参数
      yyxx_form: {
        customerId:'',
        phoneNum:undefined
      },
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "请查询用户编号", trigger: "blur" }
        ],
        phoneNum: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        appointmentTime: [
          { required: true, message: "预约服务的时间不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "客户预约的服务类型不能为空", trigger: "change" }
        ]
      },
      sureForm:{
        isCanceled:'0',
        customerId:'',
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    };
  },
  created() {
    this.showList()
  },
  methods:{
    submitForm(){
      this.$refs.form.validate((val)=>{
        if(val){
          // console.log(val)
          submitYyxx(this.yyxx_form).then(response => {
            // this.$modal.msgWarning("提交成功，请确认是否预约")
            this.$confirm('请确认是否预约?取消则不预约', '确认预约提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.sureForm.customerId = this.yyxx_form.customerId
              this.sureForm.isCanceled = '0'
              make_sure_submit(this.sureForm).then((res)=>{
                this.$message({
                  type: 'success',
                  message: '确认预约成功!'
                });
              })
            }).catch(() => {
              this.sureForm.customerId = this.yyxx_form.customerId
              this.sureForm.isCanceled = '1'
              make_sure_submit(this.sureForm).then((res)=>{
                this.$message({
                  type: 'info',
                  message: '已取消预约'
                });
              })

            });
          });
        }else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    cancel(){
      this.resetForm("form");
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerId: null,
        appointmentTime: null,
        serviceType: null,
        isCanceled: null,
        technicianId: null
      };
      this.resetForm("form");
    },
    // 根据手机号查询Id
    queryCustomerIdByPhone(){
      if(this.yyxx_form.phoneNum==undefined){
        this.$modal.msgError('手机号不可为空!')
      }else {
        getCustomerId(this.yyxx_form.phoneNum).then(response => {
          this.$forceUpdate(this.yyxx_form.customerId = response)
        });
      }
    },
    queryCustomerIdByPhone2(){
      if(this.queryYYXX_Params.phoneNum==undefined){
        this.$modal.msgError('手机号不可为空!')
      }else {
        getCustomerId(this.queryYYXX_Params.phoneNum).then(response => {
          this.$forceUpdate(this.queryYYXX_Params.customerId = response)
        });
      }
    },

    // 预约管理的方法
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerId = row.customerId || this.ids
      getCustomer_appointments(customerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户预约信息";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerIds = row.customerId || this.ids;
      this.$modal.confirm('是否确认删除客户预约信息编号为"' + customerIds + '"的数据项？').then(function() {
        return delCustomer_appointments(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryYYXX_Params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询客户预约信息列表 */
    getList() {
      this.loading = true;
      listCustomer_appointments(this.queryYYXX_Params).then(response => {
        this.customer_appointmentsList = response.rows;
        // console.log(this.customer_appointmentsList)
        for (let i = 0; i < this.customer_appointmentsList.length; i++) {
          // console.log(this.customer_appointmentsList[i])
          var item = this.customer_appointmentsList[i]
          item.isCanceled = this.isCanceledTableShow[item.isCanceled]
          item.serviceType = this.serviceTypeTableShow[item.serviceType]
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    showList(){
      this.getList();
    },
    /** 提交按钮 */
    submitFormYYGL() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId != null) {
            updateCustomer_appointments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer_appointments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
